package com.jd.org.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Department;
import com.jd.org.query.DepartmentQuery;
import com.jd.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个" )
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个" )
    public AjaxResult del(@PathVariable("id")Long id){
            departmentService.del(id);
            return new AjaxResult();
    }

    /**
     * 添加或者修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public AjaxResult addOrUpdate(@RequestBody @Validated Department department){
            if (department.getId() == null){
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody Long[] ids){
            departmentService.patchDel(ids);
            return new AjaxResult();
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询 + 高级查询" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 部门树查询
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树" )
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
